package com.example.demo.controller;

import com.example.demo.model.Student;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class ParamController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello, Spring Boot!";
    }

    // 1. 传递一个参数
    // 包装类 - String
    @RequestMapping("/r1")
    public String r1(String name){
        return "name = " + name;
    }
    // 包装类 - Integer
    @RequestMapping("/r2")
    public String r2(Integer age){
        return "age = " + age;
    }
    // 基本类型 - int
    @RequestMapping("/r3")
    public String r3(int age){
        return "age = " + age;
    }

    // 2. 传递多个参数
    // 传递 2 个参数
    @RequestMapping("/r4")
    public String r4(String name, Integer age){
        return "name = " + name + ", age = " + age;
    }
    // 传递一个对象
    @RequestMapping("/r5")
    public String r5(Student student){
        return student.toString();
    }

    // 3. 参数重命名
    // 必须传参
    @RequestMapping("/r6")
    public String r6(@RequestParam("name") String userName){
        return "userName = " + userName;
    }
    // 非必须传参
    @RequestMapping("/r7")
    public String r7(@RequestParam(value = "name", required = false) String userName){
        return "userName = " + userName;
    }
    // 4. 传递数组
    // 使用数组接收
    @RequestMapping("/r8")
    public String r8(String[] array){
        return Arrays.toString(array);
    }
    // 使用顺序表接收
    @RequestMapping("/r9")
    public String r9(@RequestParam ArrayList<String> array){
        return array.toString();
    }
    // 使用接口接收
    @RequestMapping("/r10")
    public String r10(@RequestParam List<String> list){
        return list.toString();
    }

    // 5. 传递 Json 对象
    @RequestMapping("/r11")
    public String r11(@RequestBody Student student){
        return student.toString();
    }

    // 6. 获取 URL 中的参数
    @RequestMapping("/r12/{id}/{name}")
    public String r12(@PathVariable("id") Integer id, @PathVariable("name") String name){
        return "id = " + id + ", name = " + name;
    }

    @RequestMapping("/r13/{userId}/{userName}")
    public String r13(@PathVariable("userId") Integer id, @PathVariable("userName") String name){
        return "id = " + id + ", name = " + name;
    }

    // 7. 上传文件
    @RequestMapping("/r14")
    public String r14(@RequestPart("file")MultipartFile f){
        return f.getOriginalFilename();
    }
}
